package freedom.sample.tcp;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ThreadPoolExecutor;

public class ServerApp 
{
	private boolean run = false;
	private ThreadPoolExecutor threadPoolExecutor;
	
    public void init() throws IOException
    {
    	ServerSocket server = null;
    	try {
    		InetAddress bindAddr = null;
    		int port = 0;
    		int backlog = 0;//客户端连接数限制
    		server = new ServerSocket(port, backlog, bindAddr);
    		int soTimeout = 0;//读时限
    		server.setSoTimeout(soTimeout);
    		Socket client = null;
    		while(run) {
    			client = server.accept();
    			if(client != null) {
    				threadPoolExecutor.execute(new RequestHandler(client));
    			}
    		}
			
		} finally {
			if(server != null)
				server.close();
		}
    }
}
